import type { Module } from 'vuex'
import type { RootState } from '../types'
import request from '@/public/request'

/**
 * 选择框所需的所有数据
 */
export interface LayoutState {
  // 基础人员数据
  userList: Array<any>
  // 运输条款
  dictData: object
  // 海运出口基础数据
  baseData: object
}
const module: Module<LayoutState, RootState> = {
  namespaced: true,
  state: {
    // 基础人员数据
    userList: [],
    // 运输条款
    dictData: {},
    // 海运出口基础数据
    baseData: {}
  },
  getters: {},
  mutations: {
    /**
     * 设置基础人员数据
     * @param state
     * @param userList
     */
    setUserList(state, userList) {
      state.userList = userList
    },
    /**
     * 设置基础人员数据
     * @param state
     * @param dictData
     */
    setDictData(state, dictData) {
      state.dictData = dictData
    },
    /**
     * 设置基础人员数据
     * @param state
     * @param marineExportData:{key:键名，data:数据}
     */
    setBaseData(state, marineExportData) {
      state.baseData = marineExportData
    }
  },
  actions: {
    /**
     * 获取业务信息的字典和基础人员的信息
     */
    getAllData({ commit, dispatch }) {
      dispatch('getBusinessData')
      dispatch('getBusinessDictData')
      dispatch('getSectionBaseData')
    },

    /**
     * 获取业务信息基础人员数据
     */
    async getBusinessData({ commit }) {
      let useList = await request.get('/api/user/userList')
      let options = useList.map((item) => {
        return {
          id: item.id,
          value: item.id,
          label: item.username,
          selectLabel: item.username
        }
      })
      commit('setUserList', options)
    },

    /**
     * 获取所有字典数据
     */
    getBusinessDictData({ commit }) {
      const listDict = [
        'sys_transterm',
        'sys_payment',
        'sys_transport',
        'sys_businessStatus',
        'sys_cargoSource',
        'sys_bl_type',
        'sys_settle_type'
      ]
      request
        .post('/api/dictDetail/queryAllLabelAsync', {
          idArray: listDict
        })
        .then((res) => {
          let dictData = {}
          for (const iterator of listDict) {
            if (iterator != 'sys_cargoSource') {
              dictData[iterator] = res[iterator].map((item) => {
                return {
                  id: item.id,
                  value: item.label,
                  label: item.label,
                  selectLabel: item.label
                }
              })
            }
          }
          commit('setDictData', dictData)
        })
      // };
    },

    // /**
    //  * 获取海运出口的部分的基础数据,获取全部数据
    //  */
    async getSectionBaseData({ commit }) {
      const baseUrl = [
        {
          url: '/api/csshippingterm/query',
          key: 'transterm'
        },
        {
          url: '/api/cspaymentmode/query',
          key: 'CsPaymentMode'
        }
        // {
        //   url: '/api/csshippingterm/query',
        //   key: 'transterm'
        // }
      ]
      let dataObj = {}
      for (const iterator of baseUrl) {
        let res = await request.get(iterator.url)
        switch (iterator.key) {
          case 'transterm':
            res.content.map((item) => {
              item.label = item.descriptionEn
              item.value = item.descriptionEn
              item.selectLabel = item.descriptionEn
            })
            break
          case 'CsPaymentMode':
            res.content.map((item) => {
              item.label = item.modeCode
              item.value = item.modeCode
              item.selectLabel = item.modeCode
            })
            break
        }
        dataObj[iterator.key] = res.content
      }
      commit('setBaseData', dataObj)
    }
  }
}

export default module
